//
//  LXDSpecialCollectionViewCell.m
//  BLDMusics
//
//  Created by lxd on 16/3/22.
//  Copyright © 2016年 李晓丹. All rights reserved.
//

#import "LXDSpecialCollectionViewCell.h"

@interface LXDSpecialCollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, UIScrollViewDelegate>
@property (nonatomic, retain) UICollectionView *collection;
/**
 *  专辑数据
 */
@property (nonatomic, retain) NSMutableArray *modelArr;
/**
 *  数量
 */
@property (nonatomic, retain) NSString *count;
/**
 *  用来接收第一层的tableView来改变滑动效果
 */
@property (nonatomic, retain)  UIScrollView *scroll;
/**
 *  接口的变化值
 */
@property (nonatomic, assign) NSInteger page;

@end

@implementation LXDSpecialCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createCollection];
    }
    return self;
}

- (void)setID:(NSNumber *)ID {
    _ID = ID;
    [self dataHandle];
    [self addFooter];
}

- (void)createCollection {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    layout.minimumInteritemSpacing = 10;
    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    self.collection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:layout];
    _collection.backgroundColor = [UIColor whiteColor];
    _collection.dataSource = self;
    _collection.delegate = self;
    [self addSubview:_collection];
    _collection.contentOffset = CGPointMake(0, 0);
    
    [_collection registerClass:[LXDSingerCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    _page =1;
    self.modelArr = [NSMutableArray array];
}


#pragma mark - 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _modelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LXDSingerCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    LXDSpecialModel *model = _modelArr[indexPath.row];
    cell.specialModel = model;
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    BYJCarouselViewController *vc = [[BYJCarouselViewController alloc] init];
    vc.value = [NSString stringWithFormat:@"%@", self.ID];
    [[self getViewController] pushViewController:vc animated:YES];
    
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake((SCREEN_WIDTH - 30) / 2, (SCREEN_WIDTH - 30) / 2+ 20);
}


#pragma mark - 数据解析
- (void)dataHandle {
    NSString *interface =[NSString stringWithFormat:@"http://api.dongting.com/song/singer/%@/albums?page=%ld", _ID, _page];
    [APPTools GETWithURL:interface par:nil success:^(id responseObject) {
        NSArray *tempArr = [LXDSpecialModel modelHandleWithArray:responseObject[@"data"]];
        [self.modelArr addObjectsFromArray:tempArr];
        [_collection reloadData];
        [_collection.mj_footer endRefreshing];
        if (tempArr.count == 0) {
            [_collection.mj_footer endRefreshingWithNoMoreData];
        }
    } filed:^(NSError *error) {
        
    }];
}
- (void)addFooter {
    // 上拉刷新-默认
    self.collection.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        _page ++;
        [self dataHandle];
    }];
    _collection.mj_footer.automaticallyHidden = YES;
}

// 获取当前navigation
- (UINavigationController*)getViewController {
    
    for (UIView* next = [self superview];next; next = next.superview) {
        
        UIResponder* nextResponder = [next nextResponder];
        
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UINavigationController *)nextResponder;
        }
        
    }
    return nil;
}

@end
